<template>
  <div class="headerimg">
    <el-container>
      <el-header>
        <p class="special">欢迎使用学籍管理系统</p>
      </el-header>
      <el-main>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        password: "enze666",
        username: "enze666"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度为 3 到 10 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "请选择密码", trigger: "change" }]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const _this = this;
          this.$axios.post("http://localhost:8081/login", this.ruleForm).then(res => {
            //const token = res.headers["authorization"];
            console.log(res.data);
            const jwt = res.headers["authorization"]
            const userInfo = res.data.data
            // 把数据共享出去
            _this.$store.commit("SET_TOKEN", jwt)
            _this.$store.commit("SET_USERINFO", userInfo)
            // 获取
            console.log(_this.$store.getters.getUser)
            _this.$router.push("/Home")
          });
        } else {
          console.log("error submit!!")
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
};
</script>

<style scoped>
.headerimg {
  background: url("../assets/preview.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  margin-top: -10px; /*上边距*/
  margin-left: -10px; /*左边距*/
  background-size: 100% 100%;
  position: fixed;
}

.special {
  font-weight: bold;
  font-size: 35px;
  display: inline-block;
  margin-top: 170px;
  color: rgb(109, 19, 104);
}

.el-header,
.el-footer {
  /* background-color: #b3c0d1;
  color: #333; */
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /* background-color: #e9eef3; */
  /* color: #333; */
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.demo-ruleForm {
  font-weight: bold;
  width: 500px;
  margin: 200px 450px;
}
</style>
